﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.MaHaiBo.Repository
{
    /// <summary>
    /// 仓储
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    public class BaseRepository<T>: IRepository<T> where T : class, new()
    {
        /// <summary>
        /// 引用上下文
        /// </summary>
        EFDbContext db;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db">上下文</param>
        public BaseRepository(EFDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>返回数据信息</returns>
        public async Task<T> GetModelAsync(int id)
        {
            try
            {
                return await db.Set<T>().FindAsync(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示全部数据信息
        /// </summary>
        /// <returns>返回数据信息集合</returns>
        public async Task<List<T>> GetAllAsync()
        {
            try
            {
                return await db.Set<T>().ToListAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据条件查询多条数据信息
        /// </summary>
        /// <param name="expre">查询条件</param>
        /// <returns>返回数据信息集合</returns>
        public async Task<List<T>> GetAsync(Expression<Func<T, bool>> expre)
        {
            try
            {
                return await db.Set<T>().Where(expre).ToListAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加数据信息
        /// </summary>
        /// <param name="expre">要添加的数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddAsync(T expre)
        {
            try
            {
                await db.Set<T>().AddAsync(expre);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改数据信息（逻辑删除）
        /// </summary>
        /// <param name="expre">修改后的数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateAsync(T expre)
        {
            try
            {
                db.Set<T>().Update(expre);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量修改相同数据信息（批量逻辑删除）
        /// </summary>
        /// <param name="values">要修改的数据编号集合</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> ByUpdateAsync(List<T> values)
        {
            db.Set<T>().UpdateRange(values);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 真删除
        /// </summary>
        /// <param name="expre">要删除的数据信息</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> DeleteAsync(T expre)
        {
            try
            {
                db.Set<T>().Remove(expre);
                return db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 不异步显示
        /// </summary>
        /// <returns></returns>
        public List<T> GetAll()
        {
            try
            {
                return db.Set<T>().ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
